<?php

class Group
{
    private $apiurl;
    private $apis;
    private $action;//$req['op']跳转的函数
    private $cid 	= 0;
    private $cname	= '';
    private $introduction = '';
    private $icon_large = '';
    private $icon_small = '';
    private $display    = 0 ;
    private $start = 0;
    private $length = 20;
    private $webdomain = '';
    public function __construct($apiurl)
    {
        global $_SC;
        $this->webdomain = $_SC['webdomain'];
        $this->apiurl   = $apiurl;
        $this->apis     = array(
           'getGroupCategories','manageGroupCategories','createGroupCategory','getGroupCategoryDetail','groupList',
            'getAllGroup','findGroup','pauseGroup','warning','removeGroup','recommendGroup','setTopGroup','getRecommendGroups',
            'removeRecommendGroup','removeTopGroup','removePauseGroup','getGroupInfo','editGroup'
        );
    }
    
    public function execute ($action)
    {
        if (empty($action)){
        	return false;
        }
        
        if (!in_array($action, $this->apis)){
        	return false;
        }
    
        $this->action = $action;
        $result = $this->$action(request());

        return $result;
    }

    private function getAllGroup($req)
    {
        $req['page'] = $req['page'] ? abs(intval($req['page'])) : 1;
        $param  = $this->filterArgumentsOfPassed($req);

        $result = requestApi($param);
        $req['totalPage'] = $result['allGrout'];
        $pageUrl = $this->pageUrl($req);
        return render('allGroup',array('list'=>$result['groups'], 'category' => $result['category'], 'pageUrl' => $pageUrl, 'webdomain' => $this->webdomain));
    }

    //更改
    private function editGroup($req)
    {
        $req['mUids'] = '';
        if(!empty($req['mUid1'])) $req['mUids'] = $req['mUid1'];
        if(!empty($req['mUid2']) && $req['mUids'] == '') $req['mUids'] = $req['mUid2'];
        if(!empty($req['mUid2']) && !empty($req['mUids'])) $req['mUids'] .= ','.$req['mUid2'];
        $param = $this->filterArgumentsOfPassed($req);

        $result = requestApi($param);
        if($result) {
            return true;
        } else {
            return false;
        }
    }


    //封禁
    private function pauseGroup($req)
    {
        $param = $this->filterArgumentsOfPassed($req);

        $result = requestApi($param);
        if($result) {
            return true;
        } else {
            return false;
        }
    }

    private function removePauseGroup($req)
    {
        $param = $this->filterArgumentsOfPassed($req);

        $result = requestApi($param);
        if($result) {
            return true;
        } else {
            return false;
        }
    }

    private function warning($req) {
        $param = $this->filterArgumentsOfPassed($req);

        $result = requestApi($param);
        if($result) {
            return true;
        } else {
            return false;
        }
    }

    private function removeGroup($req) {
        $param = $this->filterArgumentsOfPassed($req);

        $result = requestApi($param);
        if($result) {
            return true;
        } else {
            return false;
        }
    }

    private function findGroup($req)
    {
        $req['page'] = $req['page'] ? abs(intval($req['page'])) : 1;
        $option = $req['option'];
        if($option == 1) {
            $req['name'] = $req['text'];
            $req['gid'] = 0;
        }else{
            $req['gid'] = $req['text'];
            $req['name'] = '';
        }
        $param  = $this->filterArgumentsOfPassed($req);

        $result = requestApi($param);
        $req['totalPage'] = $result['allGrout'];
        $pageUrl = $this->pageUrl($req);
        return render('allGroup',array('list'=>$result['groups'], 'category' => $result['category'], 'pageUrl' => $pageUrl, 'webdomain' => $this->webdomain));
    }

    private function recommendGroup ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (empty($res)) return false;

        return true;
    }

    private function removeRecommendGroup ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (empty($res)) return false;

        return true;
    }

    private function getRecommendGroups($req) {
        $param = $this->filterArgumentsOfPassed($req);

        $result = requestApi($param);
        $req['totalPage'] = $result['allCount'];
        $pageUrl = $this->pageUrl($req);
        return render('allGroup',array('list'=>$result['groups'], 'category' => $result['category'], 'pageUrl' => $pageUrl, 'webdomain' => $this->webdomain));
    }

    private function setTopGroup($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $result = requestApi($param);
        if($result) {
            return true;
        } else {
            return false;
        }
    }
    private function removeTopGroup($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $result = requestApi($param);
        if($result) {
            return true;
        } else {
            return false;
        }
    }

    
    private function createGroupCategory($req)
    {

    	$param  = $this->filterArgumentsOfPassed($req);
    	    	
    	$result = requestApi($param);
		
    	if($result){
    		return 1;
    	}
    	return 0;
    }
    
    private function manageGroupCategories($req)
    {
    	
    	$param  = $this->filterArgumentsOfPassed($req);
    	$result = requestApi($param);
    	
    	if($result){
    		return 1;
    	}
    	return 0;
    	
    }
    
    private function getGroupCategories($req)
    {
    	$param  = $this->filterArgumentsOfPassed($req);
    	
    	$result = requestApi($param);
		$res    = $result['cats'];
    		    	 
    	return render('groupCategory',array('list'=>$res));
    }

//    群组升级列表
    private function groupList($req)
    {
        $req['page'] = $req['page'] ? abs(intval($req['page'])) : 1;
        $label  = $req['label'];
        $status = $req['status'];
        $attribute = 0;
        if ($status == 1 && $label == 1) $attribute = 101;
        if ($status == 1 && $label == 2) $attribute = 201;
        if ($status == 2 && $label == 1) $attribute = 1;
        if ($status == 2 && $label == 2) $attribute = 2;
        $req['attribute'] = $attribute;
        $param  = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $res['level']['label'] = $label;
        $res['level']['status'] = $status;
        $labelArr  = array( $label     => ' selected' );
        $statusArr = array( $status     => ' checked' );
        // 记录总数设为500条
        $total   = 500;
        $req['totalPage'] = ceil($total / $this->length);
        $pageUrl = $this->pageUrl($req);
        return render('groupList',array('list'=>$res['groups'], 'status'=>$res['level'], 'pageUrl' => $pageUrl, 'labelArr'=>$labelArr , 'statusArr'=>$statusArr));
    }


    private function getGroupInfo ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (empty($res)) return false;
        return render('groupmodal', $res['group_info']);
    }
  
    private function getGroupCategoryDetail($req)
    {
    	$param  = $this->filterArgumentsOfPassed($req);
    	
    	$result = requestApi($param);
    	$result['status_css'] = array($result['status']=>'selected');
    	
    	header("Content-type: application/json");
    	
    	return JsonFormat($result);
    }

    /**
     * 过滤传参
     * @param $req
     * @return array
     */
    private function filterArgumentsOfPassed($req)
    {
    	$param = array();
        switch ($this->action){
        	case "getGroupCategories":
        		$param = array(
        			'do' 		=> 'groupService',
        			'action'	=> 'getGroupCategories',
        			'format'	=> 'json'
        		);
        		break;
        	case "getGroupCategoryDetail":
        		$param = array(
        				'do' 		=> 'groupService',
        				'action'	=> 'getGroupCategoryDetail',
        				'format'	=> 'json',
        				'cid'		=> $this->validateParam('cid',$req['cid'])
        		);
        		break;
        	case 'manageGroupCategories':
        		$param = array(
        				'do'		=> 'groupService',
        				'action'	=> 'manageGroupCategories',
        				'format'	=> 'json',
        				'cid'		=> $this->validateParam('cid',$req['cid']),
        				'status'	=> $this->validateParam('status', $req['status']),
        				'cname'		=> $this->validateParam('cname',  trim($req['cname'])),
        				'introduction' => $this->validateParam('introduction', trim($req['introduction'])),
        				'icon_large'   => $this->validateParam('icon_large',$req['icon_large']),
        				'display'	   => $this->validateParam('display',$req['display'])
        				
        		);
        		break;
        	case 'createGroupCategory':
        		$param = array(
        				'do'		=> 'groupService',
        				'action'	=> 'createGroupCategory',
        				'format'	=> 'json',
        				'cid'		=> $this->validateParam('cid',$req['cid']),
        				'status'	=> $this->validateParam('status', $req['status']),
        				'cname'		=> $this->validateParam('cname',  trim($req['cname'])),
        				'introduction' => $this->validateParam('introduction', trim($req['introduction'])),
        				'icon_large'   => $this->validateParam('icon_large',$req['icon_large']),
        				'display'	   => $this->validateParam('display',$req['display'])
        		
        		);
        		break;
            case 'groupList':
        		$param = array(
                    'do'          => 'getApplyUpgradeGroups',
                    'format'      => 'json',
                    'start'       => (intval($req['page']) - 1) * $this->length,
                    'length'      => empty($req['length']) ? $this->length : $req['length'],
                    'uid'         => intval($req['uid']),
                    'gid'         => empty($req['gid'])  ? 0 : intval($req['gid']) ,
                    'attribute'   => $req['attribute'],
                );
        		break;
            case 'getAllGroup':
                $param = array(
                    'do'          => 'group',
                    'action'	  => 'allGroup',
                    'format'      => 'json',
                    'start'       => (intval($req['page']) - 1) * $this->length,
                    'length'      => empty($req['length']) ? $this->length : $req['length'],
                );
                break;
            case 'findGroup':
                $param = array(
                    'do'          => 'group',
                    'action'	  => 'findGroup',
                    'format'      => 'json',
                    'gid'         => $req['gid'],
                    'name'        => $req['name'],
                    'start'       => (intval($req['page']) - 1) * $this->length,
                    'length'      => empty($req['length']) ? $this->length : $req['length'],
                );
                break;
            case 'pauseGroup':
                $param = array(
                    'do'          => 'group',
                    'action'      => 'pauseGroup',
                    'format'      => 'json',
                    'gid'         => $req['gid'],
                    'time'        => $req['time']
                );
                break;
            case 'warning':
                $param = array(
                    'do'          => 'group',
                    'action'      => 'warning',
                    'format'      => 'json',
                    'gid'         => $req['gid'],
                    'reason'      => $req['reason']
                );
                break;
            case 'removeGroup':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'removeGroup',
                    'format'    =>  'json',
                    'gid'       =>  $req['gid'],
                    'reason'    =>  $req['reason']
                );
                break;
            case 'recommendGroup':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'recommendGroup',
                    'format'    =>  'json',
                    'gid'       =>  $req['gid'],
                    'score'     =>  $req['score']
                );
                break;
            case 'removeRecommendGroup':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'removeRecommendGroup',
                    'format'    =>  'json',
                    'gid'       =>  $req['gid']
                );
                break;
            case 'setTopGroup':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'setTopGroup',
                    'format'    =>  'json',
                    'gid'       =>  $req['gid'],
                    'score'     =>  $req['score']
                );
                break;
            case 'removeTopGroup':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'removeTopGroup',
                    'format'    =>  'json',
                    'gid'       =>  $req['gid']
                );
                break;
            case 'removePauseGroup':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'removePauseGroup',
                    'format'    =>  'json',
                    'gid'       =>  $req['gid']
                );
                break;
            case 'getRecommendGroups':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'getRecommentGroups',
                    'format'    =>  'json',
                    'start'       => (intval($req['page']) - 1) * $this->length,
                    'length'      => empty($req['length']) ? $this->length : $req['length'],
                );
                break;
            case 'getGroupInfo':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'getGroupInfo',
                    'format'    =>  'json',
                    'gid'       =>  $req['gid']
                );
                break;
            case 'editGroup':
                $param = array(
                    'do'        =>  'group',
                    'action'    =>  'editGroup',
                    'format'    =>  'json',
                    'gid'       =>  $req['gid'],
                    'uid'       =>  $req['uid'],
                    'mUids'     =>  $req['mUids'],
                    'name'      =>  $req['name'],
                    'category'  =>  $req['category'],
                    'avatar'    =>  $req['avatar'],
                    'cover'     =>  $req['cover'],
                    'introduction'  =>  $req['introduction']
                );
                break;
        }
    
        return $param;
    }
    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
        $mpurl = "admin.php?ac=".$req['ac']."&op=".$req['op'];
        switch ($req['op']){
            case "groupList":
                $mpurl  .= '&gid='.$req['gid'].'&label='.$req['label'].'&status='.$req['status'];
                $pageurl = multitotal($req['page'], $req['totalPage'], $mpurl);
                break;
            case "findGroup":
                $mpurl  .= '&gid='.$req['gid'].'&name='.$req['name'];
                $pageurl = multitotal($req['page'], $req['totalPage'], $mpurl);
                break;
            case "getAllGroup":
                $pageurl = multitotal($req['page'], $req['totalPage'], $mpurl);
                break;
        }
        return $pageurl;
    }
    
    private function validateParam($strParam,$reqParam)
    {
    	return empty($reqParam) ? $this->$strParam : $reqParam;
    }   
}

$req = request();
$group  = new Group($_SC ['apiurl']);
echo $group->execute($req['op']);
exit;

